/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-18 11:13:21
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-20 10:46:42
 */
import 'package:flutter/material.dart';
import 'package:flutter_data_center/components/row_menu.dart';
import 'package:flutter_data_center/controller/broadcast_controller.dart';
import 'package:flutter_data_center/screens/broadcast/add/broadcast_add_screen.dart';
import 'package:flutter_data_center/utills/navigator_utils.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:line_icons/line_icons.dart';
import 'components/broadcast_list.dart';
import 'components/music_controll.dart';
import 'components/playing_music.dart';

class BroadcastScreen extends StatefulWidget {
  const BroadcastScreen({Key key}) : super(key: key);
  static String routeName = "/broadcast";

  @override
  _BroadcastScreenState createState() => _BroadcastScreenState();
}

class _BroadcastScreenState extends State<BroadcastScreen> {
  BroadcaseController _broadcastController = Get.put(BroadcaseController());
  final keyWidget = GlobalKey();
  int currentIndex = 0;
  int musicIndex = 0;

  double height = 0;
  void calculateSizeAndPosition() =>
      WidgetsBinding.instance.addPostFrameCallback((_) {
        final RenderBox box =
            keyWidget.currentContext.findRenderObject(); // 包含widget的绘制信息
        setState(() {
          height = box.size.height + 170;
        });

        // TODO 获得高度，保存高度  然后通过列表数据， 记录每个大楼层的高度，并存于一个map中 每次滑动都检测
      });
  void initState() {
    super.initState();
    calculateSizeAndPosition();
    // 发起网络请求
    _broadcastController.getPlaying();
    _broadcastController.getTaskList();
    // _broadcastController.getFileList();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: buildAppBar(),
        body: SafeArea(
          child: SingleChildScrollView(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 17),
              child: Column(
                children: [
                  Column(
                    key: keyWidget,
                    children: [
                      PlayingMusic(),
                      RowMenu(
                        currentIndex: currentIndex,
                        onChange: (value) {
                          setState(() {
                            currentIndex = value;
                          });
                        },
                        menus: _broadcastController.menus,
                        showRight: true,
                        slot: IconButton(
                          icon: Icon(LineIcons.plus),
                          onPressed: () {
                            NavigatorUtils.pushPageByFade(
                              context: context,
                              targetPage: BroadcastAddScreen(),
                              isReplace: false,
                              startMills: 500,
                            );
                          },
                        ),
                      ),
                    ],
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 0, vertical: 10),
                    height: MediaQuery.of(context).size.height - height,
                    child: BroadcastList(
                        currentIndex: musicIndex,
                        onChange: (value) {
                          setState(() {
                            musicIndex = value;
                          });
                        }),
                  ),
                  MusicControll(),
                  SizedBox(height: 10),
                ],
              ),
            ),
          ),
        ),
      );

  AppBar buildAppBar() {
    return AppBar(
      backgroundColor: Colors.transparent,
      leading: Container(
        width: 39,
        height: 39,
        padding: EdgeInsets.all(10),
        child: SvgPicture.asset(
          "assets/icon/icon_broadcast.svg",
        ),
      ),
      title: Text(
        "广播",
        style: TextStyle(
          fontSize: 24,
        ),
      ),
      elevation: 0,
    );
  }
}
